package com.angel.kuaidi.activity;

import java.util.HashMap;
import java.util.Map;

import com.angel.kuaidi.KuaidiApp;
import com.angel.kuaidi.R;
import com.angel.kuaidi.db.DB;
import com.angel.kuaidi.entity.HistoryInfo;
import com.angel.kuaidi.entity.WuliuCompanyInfo;

import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainSplashActivity extends BaseActivity {

	public static final int ANIMATION_END_MSG = 1;
	public static final int INITFILE_END_MSG = 2;

	private String curversion = "";
	private String oldversion;

	private String username, password;

	private SharedPreferences preferences;
	private int loginStatus = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);
		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);

		try {
			curversion = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 没登陆一次，都需要消费一次

		RelativeLayout logoView = (RelativeLayout) findViewById(R.id.rl_logo);

		// 透明动画（从完全透明到不透明，分别对应第一个参数和第二个参数）
		AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f);
		// 动画效果时间为3秒
		animation.setDuration(2000);
		// 动画监听
		animation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) { // 动画开始时执行此方法
				// AgentApp.getSelf().getAssetsFileManager().initFile();

			}

			@Override
			public void onAnimationRepeat(Animation animation) { // 动画重复调用时执行此方法
			}

			@Override
			public void onAnimationEnd(Animation animation) { // 动画结束时执行此方法

				boolean flag = IfaddShortCut();// 如果已经创建，则不需要在创建
				if (flag == false) {
					addShortCut();
				}

				Intent intent = new Intent(mContext, IndexActivity.class);
				startActivity(intent);
				finish();
			}
		});
		// 设置开始动画
		logoView.startAnimation(animation);

	}

	public void addShortCut() {

		// Intent shortcut = new
		// Intent("com.android.launcher.action.INSTALL_SHORTCUT");
		// // 设置属性
		// shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
		// getResources().getString(R.string.app_name));
		// ShortcutIconResource iconRes =
		// Intent.ShortcutIconResource.fromContext(mContext,
		// R.drawable.ic_icon);
		// shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconRes);
		//
		// // 是否允许重复创建
		// shortcut.putExtra("duplicate", false);
		// Intent intent = new Intent(Intent.ACTION_MAIN);
		// intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		// intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
		// intent.addCategory(Intent.CATEGORY_LAUNCHER);
		// intent.setClass(mContext, MainSplashActivity.class);
		// // 设置启动程序
		// shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
		Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
		shortcutIntent.putExtra("duplicate", false);
		Intent intent = new Intent();
		intent.setComponent(new ComponentName(getPackageName(), MainSplashActivity.class.getName()));
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
		shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_icon));
		mContext.sendBroadcast(shortcutIntent);
	}

	public boolean IfaddShortCut() {
		boolean isInstallShortcut = false;
		final ContentResolver cr = mContext.getContentResolver();
		// 本人的2.2系统是”com.android.launcher2.settings”,网上见其他的为"com.android.launcher.settings"
		String AUTHORITY = null;
		if (android.os.Build.VERSION.SDK_INT == 8) {
			AUTHORITY = "com.android.launcher2.settings";
		} else {
			AUTHORITY = "com.android.launcher.settings";
		}
		final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
		Cursor c = cr.query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?", new String[] { getString(R.string.app_name) }, null);// XXX表示应用名称。
		if (c != null && c.getCount() > 0) {
			isInstallShortcut = true;
		}
		return isInstallShortcut;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
